<template>
<div>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>客户管理</el-breadcrumb-item>
    <el-breadcrumb-item>出院登记</el-breadcrumb-item>
  </el-breadcrumb>
  <el-row :gutter="20">
    <el-col :span="4">
      <el-button type="primary" @click="openRegisterDialog">出院登记</el-button>
    </el-col>
    <el-col :span="4" :offset="16">
      <el-input placeholder="请输入姓名" clearable v-model="name" @clear="search">
        <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
      </el-input>
    </el-col>
  </el-row>
  <el-table
      :data="OlderList"
      style="width: 100%;text-align: center">
    <el-table-column label="编号" type="index" align="center"></el-table-column>
    <el-table-column
        prop="name"
        label="姓名"
        align="center">
    </el-table-column>
    <el-table-column
        prop="sex"
        label="性别"
        align="center">
    </el-table-column>
    <el-table-column
        prop="idcard"
        label="身份证号"
        align="center">
    </el-table-column>
    <el-table-column
        prop="tel"
        label="联系电话"
        align="center">
    </el-table-column>
    <el-table-column
        prop="checkdate"
        label="入住日期"
        align="center">
    </el-table-column>
    <el-table-column
        prop="exitdate"
        label="出院日期"
        align="center">
    </el-table-column>
    <el-table-column
        prop="status"
        label="在院状态"
        align="center">
      <template slot-scope="scope">
        <el-tag v-if="scope.row.status == 0">已退房</el-tag>
        <el-tag v-if="scope.row.status == 1">入住</el-tag>
        <el-tag v-if="scope.row.status == 2">待退房</el-tag>
      </template>
    </el-table-column>

    <el-table-column label="操作" align="center">
      <template slot-scope="scope">
        <el-button
            size="mini"
            v-if="scope.row.status == 2"
            @click="confirmExit(scope.row.id)">退房
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[3, 6 , 12, 24]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
  ></el-pagination>

  <!--    出院登记-->
  <el-dialog title="出院登记" :visible.sync="RegisterDialogVisible"
             width="50%" @close="RegisterFormClose" :close-on-click-modal="false">
    <el-form
        :model="RegisterForm"
        :rules="RegisterFormRules"
        ref="RegisterFormRef"
        label-width="100px"
        class="demo-ruleForm"
        inline-message="true"
    >
      <el-form-item label="长者" prop="name">
        <input style="width: 220px;text-align: center;height: 34px;     border-radius: 4px;border: 1px solid #DCDFE6;"
               v-model="RegisterForm.name" readonly @click="innerDialog" />
      </el-form-item>
      <el-form-item label="离院日期" prop="exitdate" >
        <el-date-picker
            v-model="RegisterForm.exitdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="Register">确 定</el-button>
        <el-button @click="RegisterFormClose">取 消</el-button>
      </span>
  </el-dialog>

<!-- 选择长者信息 -->
  <el-dialog title="选择长者信息" :visible.sync="innerDialogVisible" @close="innerClose" :close-on-click-modal="false" width="70%">
    <el-row :gutter="20">
      <el-col :span="6" :offset="18">
        <el-input placeholder="请输入姓名" clearable v-model="innerName" @clear="searchOlder">
          <el-button slot="append" icon="el-icon-search" @click="searchOlder"></el-button>
        </el-input>
      </el-col>
    </el-row>
    <el-table
        :data="innerOlderList"
        style="width: 100%;text-align: center">
      <el-table-column label="编号" type="index" align="center"></el-table-column>
      <el-table-column
          prop="name"
          label="姓名"
          align="center">
      </el-table-column>
      <el-table-column
          prop="sex"
          label="性别"
          align="center">
      </el-table-column>
      <el-table-column
          label="床位号"
          align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.bedId == item.id" v-for="item in bedList">{{item.bedname}}</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="idcard"
          label="身份证号"
          align="center">
      </el-table-column>
      <el-table-column
          prop="checkdate"
          label="入住日期"
          align="center">
      </el-table-column>
      <el-table-column
          prop="status"
          label="在院状态"
          align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1">入住</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="selectOlder(scope.row)">选择
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="innerHandleSizeChange"
        @current-change="innerHandleCurrentChange"
        :current-page="innerPageNum"
        :page-sizes="[3, 6 , 12, 24]"
        :page-size="innerPageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="innerTotal"
    ></el-pagination>
  </el-dialog>
</div>
</template>

<script>
export default {
  data() {
    return {
      OlderList:[],
      pageNum: 1,
      pageSize: 3,
      total:9,
      name:'',
      older:{},
      RegisterDialogVisible:false,
      RegisterForm:{
        id:0,
        name:'',
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime()+24*3600*1000 < Date.now();
        },
      },
      RegisterFormRules:{
        name: { required: true, message: "请输入姓名", trigger: "change" },
        exitdate:{required: true, message: "请选择日期", trigger: "blur" }

      },
      innerPageNum:1,
      innerPageSize:3,
      innerTotal:9,
      innerName:'',
      innerOlderList:[],
      innerDialogVisible:false,
      bedList:[],
    }
  },
  methods:{
    //出院登记
    openRegisterDialog(){
      this.RegisterDialogVisible = true;
    },
    RegisterFormClose(){
      this.$refs.RegisterFormRef.resetFields();
      this.RegisterDialogVisible = false;
    },
    Register(){
      let url = "/older/exitRegister?id="+this.RegisterForm.id + "&exitdate="+this.RegisterForm.exitdate ;
      this.$refs.RegisterFormRef.validate((val)=>{
        if(val){
          this.$axios.get(url).then((res)=>{
            if(res.data.code == 200) {
              this.$message({
                message: res.data.msg,
                type: 'success',
                duration: 1000
              });
              this.RegisterDialogVisible = false;
              this.initExitData();
            }else {
              this.$message({
                message: res.data.msg,
                type:'error',
                duration:1000
              })

            }
          })
        }else {
          alert("请完善信息");
        }
      })
    },

    //选择出院长者
    innerDialog(){
      this.innerDialogVisible = true;
      this.searchInnerOlder();
    },
    selectOlder(older){
      this.RegisterForm.id = older.id;
      this.RegisterForm.name = older.name;
      this.innerDialogVisible = false;
      this.$refs.RegisterFormRef.validateField('name');
    },
    innerClose(){
      this.innerDialogVisible = false;
    },

    //查询所有入住长者信息
    searchOlder() {
      this.innerPageNum = 1;
      this.searchInnerOlder();
    },
    searchInnerOlder(){
      this.initBedData();
      let url = "/older/selectOlderByStatus?now="+this.innerPageNum+"&size="+this.innerPageSize+"&name="+this.innerName;
      this.$axios.get(url).then((res)=>{
        if(res.data.code == 200){
          this.innerOlderList = res.data.data.list;
          this.innerTotal = res.data.data.total;
        }else {
          this.$message({
            message: res.data.msg,
            type:'error',
            duration:1000
          })
        }
      })
    },
    innerHandleSizeChange(newSize) {
      this.innerPageSize = newSize;
      this.innerPageNum = 1;
      this.searchInnerOlder();
    },
    innerHandleCurrentChange(newPage) {
      //alert(newPage);
      this.innerPageNum = newPage;
      this.searchInnerOlder();
    },
    //查询所有床位信息
    initBedData(){
      let url = "/older/selectAllBed";
      this.$axios.get(url).then((res)=>{
        if(res.data.code == 200){
          this.bedList = res.data.data;
        }else {
          this.$message({
            message: res.data.msg,
            type:'error',
            duration:1000
          })
        }
      })
    },

    //点击退房
    confirmExit(id){
      let url = "/older/confirmExit?id="+id;
      this.$axios.get(url).then((res)=>{
        if(res.data.code == 200){
          alert("已确认退房!");
          this.initExitData();
        }else {
          this.$message({
            message: res.data.msg,
            type:'error',
            duration:1000
          })
        }
      })
    },


    //查询所有出院信息
    search() {
      this.pageNum = 1;
      this.initExitData();
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.pageNum = 1;
      this.initExitData();
    },
    handleCurrentChange(newPage) {
      //alert(newPage);
      this.pageNum = newPage;
      this.initExitData();
    },
    initExitData(){
      let url = "/older/selectExitOlder?now="+this.pageNum+"&size="+this.pageSize+"&name="+this.name;
      this.$axios.get(url).then((res)=>{
        if(res.data.code == 200){
          this.OlderList = res.data.data.list;
          this.total = res.data.data.total;
        }else {
          this.$message({
            message: res.data.msg,
            type:'error',
            duration:1000
          })
        }
      })
    }
  },
  created() {
    this.initExitData();
  }
}
</script>

<style scoped>

</style>